package com.xiaohudie.JUC;

/**
 * @Author xiao hu die
 * @Date 2022/1/12 21:32
 * @Version 1.0
 */

import lombok.SneakyThrows;

/**创建资源类*/
class  Share{
   /**初始值*/
   private Integer a =0;
   /**加1的方法*/
   @SneakyThrows
   public synchronized  void add(){
      /**判断*/
      while (a!=0){
         this.wait();
      }
      a++;
      System.out.println(Thread.currentThread().getName()+"：：" +a);
      /**通知*/
      this.notifyAll();
     }
   @SneakyThrows
   public synchronized void delete(){
      /**判断*/
      while (a!=1){
         /**wait操作在哪里谁在哪里唤醒
          * wait会释放锁*/
         this.wait();
      }
      /**干活*/
      a--;
      System.out.println(Thread.currentThread().getName()+";;"+a);
      /**通知*/
      this.notifyAll();
   }

}
public class ThreadDemo1 {
   public static void main(String[] args) {
      Share share = new Share();
      new Thread(()->{
         for (int i = 0; i <=10; i++) {
            share.add();
         }
      },"AA").start();
      new Thread(()->{
         for (int i = 0; i <= 10; i++) {
            share.delete();
         }
      },"BB").start();
      new Thread(()->{
         for (int i = 0; i <=10; i++) {
            share.add();
         }
      },"CC").start();
      new Thread(()->{
         for (int i = 0; i <= 10; i++) {
            share.delete();
         }
      },"DD").start();
   }

}
